<?php
/**
 * 
 * category mannger Controller
 * @author alang
 *
 */

class Category extends Admin_controller
{
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('category_mdl','category');
		$this->load->library('form_validation');
	}
	
	public function index( $id=0 )
	{
		$this->load->model( 'category_mdl', 'category' );
		$category = array();
		$category = $this->category->get_category_by_id( $id );
		
		$data['category'] = $category;
		$this->load->view('adminroot/category',$data);
	}
	
	/**
	 * 
	 * 添加分类 ...
	 */
	public function add()
	{
		$this->_load_validation_rules();
		if ( $this->form_validation->run() == FALSE ){
			
			$this->load->view( 'adminroot/category_add' );
			
		} else {
			
			$data['name']     = $this->input->post('name');
			$data['parentid'] = $this->input->post('parentid');
			$data['rank']     = $this->input->post('rank');
			
			if ( $this->category->add_category($data) ) {
				
				redirect( 'adminroot/category' );
				
			}		
		}
		
	}
	
	/**
	 * 
	 * 删除
	 */
	public function del()
	{
		$id = intval($this->uri->rsegment(4));
		if( $id ){
			
			$this->category->del( $id );	
		}
		
		redirect( 'adminroot/category' );
	}
	
	/**
	 * 
	 * 编辑分类
	 */
	public function edit()
	{
		$id = intval($this->uri->rsegment(4));
	
		if( $id ){
			
			$category = $this->category->get_one_by_id( $id );
			$data['category'] = $category;

			$this->load->view('adminroot/category_edit', $data);
			
		}
	}
	
	/**
	 * 
	 * 修改分类
	 */
	public function update()
	{
		
		$this->_load_validation_rules();
		if ( $this->form_validation->run() == FALSE ){
			
		} else {
			
			$id = $this->input->post( 'id' );
			echo $id;
			exit;
			$data[ 'name' ] = $tis->input->post( 'name' );
			$data[ 'parentid' ] = $this->input->post( 'parentid' );
			$data[ 'rank' ] = $this->input->post( 'rank' );
			
			$config[ 'id' ] = $id;
			
			$this->category->updata( $data.$config );
			
			redirect('adminroot/category');
		}
	}
	
	/**
	 * 
	 * 表单验证
	 */
	private function _load_validation_rules()
	{
		$this->form_validation->set_error_delimiters('<li>', '</li>');
		$this->form_validation->set_rules('name','分类名称','required|xss_clean');
		$this->form_validation->set_rules('parentid','所属栏目','required');

	}
}